package com.kyuketsukistudios.mikatsuki.preloader
{
	import com.kyuketsukistudios.mikatsuki.assetmanager.AssetManager;
	import com.kyuketsukistudios.mikatsuki.data.SingletonData;
	import com.kyuketsukistudios.mikatsuki.Engine;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import org.casalib.display.CasaSprite;
	/**
	 * ...
	 * @author RZEROSTERN
	 * In the beginning I made a MC with the logo, but finally I decided to make a video with
	 * Adobe After Effects. And it was so awesome that I've changed the method for playing the
	 * video in our application.
	 */
	public class LogoRollIntro extends CasaSprite
	{
		private var m_assetManager:AssetManager;
		private var m_stageReference:Stage;
		public var m_rzerologo:MovieClip;
		public var netStream:NetStream;
		private var m_videoName:String;
		
		public function LogoRollIntro(p_videoName:String) 
		{
			m_videoName = p_videoName;
			SingletonData.getInstance().sendToConsole(0xffffff, "Loading logos...");
			m_assetManager = new AssetManager(onLibraryLoaded, "videoPlayer");
		}
		
		private function onLibraryLoaded():void 
		{
			this.m_stageReference = Engine.m_stageReference;
			SingletonData.getInstance().sendToConsole(0x00ff00, "Loading videoPlayerLib.swf");
			m_rzerologo = m_assetManager.getMyAsset("VideoPlayer", "mc");
			m_rzerologo.x = m_rzerologo.y = 0;
			m_rzerologo.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			addChild(m_rzerologo);
		}
		
		private function onEnterFrame(p_event:Event):void 
		{
			p_event.currentTarget.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			var netConnection:NetConnection = new NetConnection();
			netConnection.connect(null);
			
			netStream = new NetStream(netConnection);
			m_rzerologo.video.attachNetStream(netStream);
			
			netStream.client = this;
			netStream.bufferTime = 2;
			netStream.play("../video/" + m_videoName + ".f4v");
			netStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStreamStatusEvent);
		}
		
		public function onNetStreamStatusEvent(p_event:NetStatusEvent):void 
		{
			switch (p_event.info.code)
			{
				case "NetStream.Play.Start":
					SingletonData.getInstance().sendToConsole(0x00ff00, "Playing video");
				break;
				
				case "NetStream.Play.StreamNotFound":
					SingletonData.getInstance().sendToConsole(0xff0000, "ERROR.- Video no encontrado");
				break;
				
				case "NetStream.Play.Stop":
					SingletonData.getInstance().sendToConsole(0xff0000, "El video ya termino");
					endRoll();
				break;
				
				default:
					
				break;
			}
		}
		
		private function endRoll():void 
		{
			dispatchEvent(new Event("VIDEO_FINISHED"));
		}
		
		public function onMetaData(p_metaData:Object):void
		{
			
		}
		
	}

}